package com.soft.bams.dao;

import java.util.Calendar;

    /**
     * 泛型   任意类型，一般由大写字母  T K E
     * @param <T>  此处的T类型，代表容器中元素的类型，例如：Account
     * @param <K>  此处的K类型，代表容器的类型，例如：Account[]、List<Account>
     */
    public interface IDao<T , K> {

        //插入元素
        public boolean insert(T t);

        //根据ID和密码查询
        public T selectOne(Long id , String password);

        //根据ID查询
        public T selectOne(Long id);

        //查询所有银行账户
        public K selectAll();

        //获取ID
        public Long getId();

        //设置ID
        public void setId(Long id);

        //新增createId() 默认方法default，有利于接口升级改造
        public default Long createId(){
            //86215021国家+邮编 201302年+月 0001(序号)
            //1.获取初始ID
            String idStr = getId().toString();
            //截取年
            int year = Integer.parseInt(idStr.substring(8, 12));
            //截取月
            int month = Integer.parseInt(idStr.substring(12, 14));
            //截取计数器
            String count = idStr.substring(14);

            //2.判断截取的年、月，是否是当前的年月，若不是则赋值，并把计数器重置
            Calendar c = Calendar.getInstance();
            int nowYear = c.get(Calendar.YEAR);
            int nowMonth = c.get(Calendar.MONTH)+1;
            if(year!=nowYear&&month!=nowMonth){
                year = nowYear;
                month = nowMonth;
                count = "0001";
            }

            //3.计数器判断是否已达到9999，则归0001
            if(count.equals("9999")){
                count = "0001";
                System.out.println("您好，本月开卡名额已用完，请下个月再来！");
                return -1L;
            }

            //4.组装ID  862150212025040001
            String str = "";
            if(month<10){
                str = "86215021"+year+"0"+month+count;
            }else{
                str = "86215021"+year+month+count;
            }

            //5.设置 ID+1
            Long id = Long.valueOf(str);
            this.setId(id+1);
            return id;
        }

        //新增update()
    }
